package com.morn.fanfou;

import com.morn.fanfou.api.Fanfou;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import android.util.Log;
import android.widget.Toast;

public class ConfigActivity extends PreferenceActivity {
	private static final String TAG = "ConfigActivity";
	private Fanfou fanfou;
	private final int CLEAR_PROGRESS = 0x001;
	private ProgressDialog clearProgress;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.config);
		fanfou = ((FanfouApplication) this.getApplication()).getFanfou();

	}

	@Override
	public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
			Preference preference) {

		String key = preference.getKey();
		if ("clearCache".equals(key)) {

			this.showDialog(CLEAR_PROGRESS);

		} else if ("isUserImage".equals(key)) {

			Toast.makeText(this, key, Toast.LENGTH_SHORT).show();
		}

		return false;
	}

	@Override
	protected Dialog onCreateDialog(int id, Bundle args) {
		switch (id) {
		case CLEAR_PROGRESS:
			Log.i(TAG, "onCreateDialog");
			clearProgress = new ProgressDialog(this);
			clearProgress.setTitle(getString(R.string.clear_cache));
			clearProgress.setMessage(getString(R.string.clearing));
			clearProgress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			clearProgress.setCancelable(false);
			clearProgress.setCanceledOnTouchOutside(false);
			break;
		}
		return clearProgress;
	}

	@Override
	protected void onPrepareDialog(int id, Dialog dialog) {
		super.onPrepareDialog(id, dialog);
		switch (id) {
		case CLEAR_PROGRESS:
			Log.i(TAG, "onPrepareDialog");
			new ClearCacheTask().execute();
			break;
		}

	}

	private class ClearCacheTask extends AsyncTask<String, String, Object> {
		@Override
		protected Object doInBackground(String... params) {
			Log.i(TAG, "doInBackground");
			fanfou.clearImage();
			return null;
		}

		@Override
		protected void onPostExecute(Object result) {
			Log.i(TAG, "onPostExecute");
			clearProgress.cancel();
		}
	}

	public static boolean isShowUserPhoto(Context context) {
		SharedPreferences pref = PreferenceManager
				.getDefaultSharedPreferences(context);
		return pref.getBoolean("isShowUserPhoto", true);
	}

	public static boolean isShowImage(Context context) {
		SharedPreferences pref = PreferenceManager
				.getDefaultSharedPreferences(context);
		return pref.getBoolean("isShowImage", true);
	}

	public static boolean isImageAuto(Context context) {
		SharedPreferences pref = PreferenceManager
				.getDefaultSharedPreferences(context);
		return pref.getBoolean("isImageAuto", true);
	}

	public static boolean isImageSizeBig(Context context) {
		SharedPreferences pref = PreferenceManager
				.getDefaultSharedPreferences(context);
		return pref.getBoolean("isImageSizeBig", true);
	}
}
